package com.mcxx.modules.miniSalvation.order.service.read;

import com.mcxx.modules.filemanage.file.service.FileService;
import com.mcxx.modules.miniSalvation.check.dto.CheckFileDTO;
import com.mcxx.modules.miniSalvation.order.dao.read.OrderCertReadDao;
import com.mcxx.modules.miniSalvation.order.dto.CheckDTO;
import com.mcxx.modules.miniSalvation.order.dto.NoticeBoardDTO;
import com.mcxx.modules.miniSalvation.order.dto.OrderCertDTO;
import com.mcxx.modules.miniSalvation.order.entity.OrderCertEntity;
import com.mcxx.modules.miniSalvation.order.param.ReferenceParam;
import com.mcxx.modules.temporary.order.entity.TmpOrderCertEntity;
import com.yinhai.ta404.core.service.BaseService;
import com.yinhai.yhdcs.core.common.DcsServerException;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;


@Service("orderCertReadService")
public class OrderCertReadService extends BaseService {
    @Autowired
    private OrderCertReadDao orderCertReadDao;
    @Autowired
    private FileService fileService;


    public List<Map<String, Object>> countByTypes(ReferenceParam countParam) {
        List<Map<String, Object>> countResult = orderCertReadDao.countByTypes(countParam);
        if (countResult.size() == countParam.getTypes().length) {
            return countResult;
        }

        Set<String> types = new HashSet<>(Arrays.asList(countParam.getTypes()));
        if (countResult.size() == 0) {
            fillCountResult(types,countResult);
            return countResult;
        }


        Set<String> actTypes = new HashSet<>();
        countResult.forEach(countMap -> {
            actTypes.add((String) countMap.get("type"));
        });


        types.removeAll(actTypes);

        fillCountResult(types,countResult);

        return countResult;
    }


    private void fillCountResult(Set<String> types,List<Map<String,Object>> result) {
        types.forEach(type -> {
            HashMap<String, Object> map = new HashMap<>();
            map.put("type",type);
            map.put("count",0);
            result.add(map);
        });
    }


    public List<Map<String, String>> listByType(ReferenceParam referenceParam) {
        List<Map<String, String>> list = orderCertReadDao.listByType(referenceParam);
//        list.forEach(map -> {map.put("url", BASE_PATH + map.get("url")); });
        list.forEach(map -> {
            try {
                String imgUrl = fileService.getFileUrl(map.get("url"),map.get("bucketId"));
                map.put("url", imgUrl);
            } catch (DcsServerException e) {
                map.put("url", "#");
            }
        });

        return list;
    }


    public String getFileKeyById(String id) {
        return orderCertReadDao.getFileKeyById(id);
    }


    public List<String> listFileKeyByIds(String[] ids) {
        return orderCertReadDao.listFileKeyByIds(ids);
    }


    public List<NoticeBoardDTO> cntNoticesCertNum(List<String> batchNoList){
        return orderCertReadDao.cntNoticesCertNum(batchNoList);
    }


    public OrderCertDTO getFileKeyAndUserLevelById(String id) {
        return orderCertReadDao.getFileKeyAndUserLevelById(id);
    }

    public List<CheckFileDTO> getFileByMemberId(String memberId,String orderId){
        return  orderCertReadDao.getFileByMemberId(memberId,orderId);
    }

    public List<CheckFileDTO> getAuthorFile(String fileType,String orderId){
        return orderCertReadDao.getAuthorFile(fileType,orderId);
    }

    public List<CheckFileDTO> getFileBySupportId(String type,String fileType,String orderId,String tableId){
        return orderCertReadDao.getFileBySupportId(type, fileType,orderId,tableId);
    }



    public List<NoticeBoardDTO> cntNoticeIdCertNum(String batchId){ return orderCertReadDao.cntNoticeIdCertNum(batchId); }


    public Map<String, Long> cntFamilyCertNum(String orderId, String familyId){
        return orderCertReadDao.cntFamilyCertNum(orderId, familyId);
    }


    public int cntInhouseCertNum(String orderId, String orderInhouseId){
        return orderCertReadDao.cntInhouseCertNum(orderId, orderInhouseId);
    }



    public int cntCaregiverCertNum(String orderId,String caregiverId){
        return orderCertReadDao.cntCaregiverCertNum(orderId,caregiverId);
    }


    public int cntBankCertNum(String memberId,String orderId){
        return orderCertReadDao.cntBankCertNum(memberId,orderId);
    }


    public int cntCertNum(String memberId,String orderId,String fileType,String accTable){
        return orderCertReadDao.cntCertNum(memberId,orderId,fileType,accTable);
    }


    public int cntCertNumTmp(String memberId,String orderId,String fileType,String accTable){
        return orderCertReadDao.cntCertNumTmp(memberId,orderId,fileType,accTable);
    }


    public int cntBankTempCertNum(String memberId,String orderId){
        return orderCertReadDao.cntBankTempCertNum(memberId,orderId);
    }


    public List<String> cntMemberIdcardNum(String orderId, String familyId){
        return orderCertReadDao.cntMemberIdcardNum(orderId, familyId);
    }

    public List<String> cntSupportIdCardNum(String orderId,String familyId){
        return orderCertReadDao.cntSupportIdCardNum(orderId,familyId);
    }

    public List<String> cntSupportCertBeforeCheck(List<CheckDTO> checkFileList){
       List<String> ids =  checkFileList.stream().map(o->o.getOrderId()).collect(Collectors.toList());
       return orderCertReadDao.cntSupportCertBeforeCheck(ids);
    }


    public int queryCertIsExiten(@Param("orderIdList") List<String> orderIdList, String fileType, String associationTable){
        return orderCertReadDao.queryCertIsExiten(orderIdList,fileType,associationTable);
    }


    public List<String> getSQSNumByOrderIds(List<String> orderIdList){


        List<String> existSQSOrderIdList = orderCertReadDao.selectFamilyMustCert(orderIdList);

        List<String> noExistCertIrderIdList = orderIdList.stream().filter(orderId->existSQSOrderIdList.contains(orderId)).collect(Collectors.toList());

        return  noExistCertIrderIdList;
    }



    public List<String> getOrderIdNoIdcardCert(List<String> familyIdList){
        return orderCertReadDao.getOrderIdNoIdcardCert(familyIdList);
    }


    public int	selectSupportAuthor(String familyId,String orderId){
        return orderCertReadDao.selectSupportAuthor(familyId,orderId);
    }


    public int	selectSupportInfo(String familyId){
        return orderCertReadDao.selectSupportInfo(familyId);
    }


    public List<String> selectSupportAuthorList(List<String> orderIdList){
        return orderCertReadDao.selectSupportAuthorList(orderIdList);
    }


    public List<OrderCertEntity> selectBySelective(OrderCertEntity orderCertEntity){
        return orderCertReadDao.selectBySelective(orderCertEntity);
    }

    public List<String> getIntegrateAuthorFile(String fileType,String orderId){
        return orderCertReadDao.getIntegrateAuthorFile(fileType,orderId);
    }

    public List<String> selectOrderNoAuthorFile(List<String> list){
        return orderCertReadDao.selectOrderNoAuthorFile(list);
    }

    public List<String> getHistoryOrderIdNoIdcardCert(List<String> familyIdList){
        return orderCertReadDao.getHistoryOrderIdNoIdcardCert(familyIdList);
    }



    public List<OrderCertEntity> selectDatumByOrderIdAndFileType(String orderId, String fileType, Integer type) {
        return orderCertReadDao.selectDatumByOrderIdAndFileType(orderId, fileType, type);
    }

    public List<String> selectDepartmentMustCert(List<String> list){
        return orderCertReadDao.selectDepartmentMustCert(list);
    }
}
